import { createVNode } from "vue";
import TableBoxVue from "./table-box.vue";
import {
	LoadChildTableOptions,
	TableBox,
	TableColsOptions,
	TableOperateType,
	TableOptions,
} from "./type";
import { BaseComponent } from "../type";
import { uniqueArr } from "@/utils";
import { TableInstance } from "element-plus";
import { BASE_PAGE_SIZE } from "../config";

type PageType = { page: number; pageSize: number };

export class BasicTaleBox<T> implements BaseComponent, TableBox<T> {
	protected tableOptions!: TableOptions<T>;
	private tableRef!: TableInstance;
	/** 请求信息 */
	private pageConfig!: PageType;
	private request!: (
		pageConfig: PageType,
		url?: string,
		/**额外的参数 */
		params?: Record<string, any>
	) => any;
	/** 请求信息end */
	constructor(opt?: TableOptions<T>) {
		this.init(); // 初始化配置
		opt && (this.tableOptions = { ...this.tableOptions, ...opt }); //合并传入的配置
	}

	protected init() {
		this.tableOptions = {
			source: [],
			columns: [],
			opers: [],
			selectedRows: [],
			loading: false,
			paginator: {
				page: 1,
				pageSize: BASE_PAGE_SIZE,
				total: 0,
				show: true,
			},
		};
	}
	showLoading() {
		this.tableOptions.loading = true;
	}
	hideLoading() {
		this.tableOptions.loading = false;
	}
	getVNode() {
		return createVNode(TableBoxVue, {
			configure: { ...this.tableOptions },
			init: (tableRef: TableInstance) => {
				this.tableRef = tableRef;
			},
		});
	}
	// 重写配置
	setTableOption(opt?: TableOptions<T>) {
		this.init(); // 初始化配置
		opt && (this.tableOptions = { ...this.tableOptions, ...opt }); //合并传入的配置
	}

	setTableSource(data: Array<T>): void {
		// function setLevel(arr, levelNum) {
		// 	for (let j = 0; j < arr.length; j++) {
		// 		const element = arr[j];
		// 		if (element.childNode.length == 0) {
		// 			element["level"] = levelNum;
		// 		} else {
		// 			element["level"] = levelNum;

		// 			setLevel(element.childNode, levelNum + 1);
		// 		}
		// 	}
		// }
		// setLevel(dataArr, 1);

		this.tableOptions.source = data.slice();
	}
	getTableSource(): Array<T> {
		return this.tableOptions.source!;
	}
	addTableCol(col: TableColsOptions<keyof T> | TableColsOptions<keyof T>[]): void {
		// 当传入的列配置参数是一个 数组 则合并，并去重
		if (col instanceof Array) {
			this.tableOptions.columns = uniqueArr<TableColsOptions<keyof T>>(
				[...this.tableOptions.columns!, ...col],
				"key"
			);
		} else {
			// 当传入的列配置参数是一个 独立 且 原数组中没有的对象，则添加至列配置
			if (!this.tableOptions.columns?.includes(col)) {
				this.tableOptions.columns?.push(col);
			}
		}
	}
	removeTableCol(key: keyof T): void {
		if (this.tableOptions.columns) {
			const removeIndex = this.tableOptions.columns.findIndex(e => e.key === key);
			removeIndex != -1 && this.tableOptions.columns.splice(removeIndex, 1);
		}
	}
	getTableColsConf(): Array<TableColsOptions<keyof T>> {
		return this.tableOptions?.columns || [];
	}
	addTableOpr(opr: TableOperateType<T>): void {
		this.tableOptions?.opers?.push(opr);
	}
	removeTableOpr(name: string): void {
		if (this.tableOptions.opers) {
			const removeIndex = this.tableOptions.opers.findIndex(e => e.name == name);
			removeIndex != -1 && this.tableOptions.opers.splice(removeIndex, 1);
		}
	}
	setTableOprDisabled(name: string, disabled: boolean): void {
		if (this.tableOptions.opers) {
			const disabledIndex = this.tableOptions.opers.findIndex(e => e.name == name);
			disabledIndex != -1 && (this.tableOptions.opers[disabledIndex].disabled = disabled);
		}
	}
	getTableOprConf(): TableOperateType<T>[] {
		return this.tableOptions.opers || [];
	}
	setTableSelection(state: boolean): void {
		this.tableOptions.selection = state;
	}
	setTableIndex(state: boolean): void {
		this.tableOptions.index = state;
	}
	setTableSelectionData(rows: Array<T>): void {
		this.tableOptions.selectedRows = rows;
	}
	getTableSelectionData(): Array<T> {
		const data = this.tableRef?.getSelectionRows && this.tableRef?.getSelectionRows();
		return data || [];
	}
	setChildTable(options: LoadChildTableOptions<T>): void {
		if (!this.tableOptions.treeProps) {
			this.tableOptions.treeProps = {} as LoadChildTableOptions<T>;
		}
		this.tableOptions.treeProps.children = options.children;
		this.tableOptions.treeProps.rowKey = options.rowKey;
		this.tableOptions.treeProps.load = options.load;
		this.tableOptions.treeProps.lazy = options.lazy;
		this.tableOptions.treeProps.hasChildren = options.hasChildren;
	}
	setTotal(total: number): void {
		this.tableOptions.paginator!.total = total;
	}
	getTotal() {
		return this.tableOptions.paginator!.total;
	}
	setDispalyPaginator(state: boolean) {
		this.tableOptions.paginator!.show = state;
	}
	/** http 相关 */
	setRequest(callback: any) {
		this.request = callback;
	}

	setPageConfig(pageConfig: PageType) {
		this.pageConfig = pageConfig;
	}

	/** 刷新 */
	refresh(url?: string) {
		const { page, pageSize } = this.pageConfig;
		this.request({ page, pageSize }, url);
	}

	requestPageData(
		page: number = this.pageConfig.page,
		pageSize: number = this.pageConfig.pageSize,
		url?: string,
		params?: Record<string, any>
	) {
		this.request({ page, pageSize }, url, params);
	}
	/** http相关 end */
}
